package cn.itsource.order.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.itsource.basic.other.config.exception.BusinessException;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.order.domain.OrderAddress;
import cn.itsource.order.domain.OrderAdopt;
import cn.itsource.order.domain.UserAddress;
import cn.itsource.order.mapper.OrderAddressMapper;
import cn.itsource.order.mapper.OrderAdoptMapper;
import cn.itsource.order.mapper.UserAddressMapper;
import cn.itsource.order.service.IOrderAdoptService;
import cn.itsource.pay.domain.OrderDto;
import cn.itsource.pay.domain.PayDto;
import cn.itsource.pay.service.impl.AlipayInfoServiceImpl;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class OrderAdoptServiceImpl extends BaseServiceImpl<OrderAdopt, BaseQuery> implements IOrderAdoptService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private PetMapper petMapper;


    @Autowired
    private AlipayInfoServiceImpl alipayInfoService;

    /**
     * 下单的方法，跳转到支付页面
     * @param payDto 有petId，addressId
     * @return 支付的html表单代码
     */
    @Override
    @Transactional
    public String submitOrder(OrderDto orderDto) {
        Long addressId = orderDto.getAddressId();
        Long petId = orderDto.getPetId();

        //1.先根据用户地址id查询到用户的地址对象
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        Optional.ofNullable(userAddress).orElseThrow(()->new BusinessException("地址不存在"));

        //2.把用户地址对象的信息拷贝到订单地址信息
        OrderAddress orderAddress = BeanUtil.copyProperties(userAddress, OrderAddress.class);
        orderAddress.setId(null);

        //3.保存订单地址表的信息，可以拿到订单地址主键id
        orderAddressMapper.insertSelective(orderAddress);

        //4.封装订单的对象
        //4.1通过宠物id查询宠物对象
        Pet pet = petMapper.selectByPrimaryKey(petId);
        Optional.ofNullable(pet).orElseThrow(()->new BusinessException("商品数据不存在"));

        //5.把订单地址主键id设置给订单对象
        OrderAdopt orderAdopt = new OrderAdopt();
        orderAdopt.setDigest("用户地址id，"+addressId+"，商品id："+petId);
        orderAdopt.setState(0);//待支付
        orderAdopt.setPrice(BigDecimal.valueOf(pet.getCostprice()));
        //雪花算法
        String orderSn = new Snowflake().nextIdStr();
        orderAdopt.setOrderSn(orderSn);
        orderAdopt.setLastConfirmTime(new Date());
        orderAdopt.setPetId(petId);
        orderAdopt.setUserId(GetUserInfoUtil.getUserId());
        orderAdopt.setShopId(pet.getShopId());
        orderAdopt.setAddressId(orderAddress.getId());

        //6.入库订单数据
        orderAdoptMapper.insertSelective(orderAdopt);

        PayDto payDto = new PayDto();
        payDto.setMoney(pet.getCostprice());
        payDto.setSubject(pet.getName());
        payDto.setSubjectDesc("宠物订单"+orderSn);
        payDto.setOrderSn(orderSn);//订单号
        //7.跳转到支付页面
        return alipayInfoService.goBuy(payDto);
    }

    @Override
    public List<OrderAdopt> findCancelOrder(String time) {
        return orderAdoptMapper.findCancelOrder(time);
    }

    public static void main(String[] args) {
        System.out.println(new Snowflake().nextIdStr());
        System.out.println(new Snowflake().nextIdStr());
        System.out.println(new Snowflake().nextIdStr());
    }
}
